package io.gitee.mojingjiao.day07;

import org.junit.Test;

import java.lang.reflect.Array;
import java.util.Arrays;

public class TestRecursive {
    @Test
    //斐波那契数列
    public void testFabonacii(){
    for(int i=0;i<10;i++){
    System.out.println(Fabonacii(i)+"   ");
}
    }
    public int Fabonacii(int index){
        if(index==0||index==1){
            return 1;
        }else{
          return Fabonacii(index-1)+Fabonacii(index-2);
        }
    }



    //递归公式计算1+2+3+...+n
    @Test
    public void testSum(){
        System.out.println(sum(100));
    }
    public int sum(int num){
        if(num==1){
            return 1;
        }else{
            return sum(num-1)+num;
        }
    }




    //求n的阶乘
    @Test
    public void testFactorial(){
        System.out.println(fac(10));
    }
    public int fac(int num){
      if (num==0){
          return 1;
      }  else{
          return fac(num-1)*num;
      }
}




//求数组中最大的数
  @Test
public void ArrayMax(){
int[] intArray={1,5,9,4,6};
     max(intArray);
  }
public void max(int[] array){
        if(array.length==2){
            int max1= array[0]>array[1]?array[0]:array[1];
            System.out.println(max1);
        }else{
           if(array[array.length-2]>array[array.length-1]){
           int[]intArray1=Arrays.copyOf(array,array.length-1);
            max(intArray1);
           }else{
               int a=array[array.length-1];
               array[array.length-1]=array[array.length-2];
               array[array.length-2]=a;
               int[]intArray2=Arrays.copyOf(array,array.length-1);
               max(intArray2);
           }
        }

}






}
